<template>
  <el-row>
    <el-col :span="6">
      <div class="app-container calendar-list-container">
        <div id="treeBox" class="filter-container">
          <el-table :data="treeList" style="width: 100%" row-key="id" border :row-style="rowStyle"
            @row-click="submitForm">
            <el-table-column prop="label" label="名称" sortable />
          </el-table>
        </div>
      </div>
    </el-col>
    <el-col :span="18">
      <div class="app-container">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">规则名称 </span>
            <el-input v-model="listQuery.name" placeholder="请输入规则名称" style="width: 200px" />
          </div>
          <div v-if="listQuery.ruleType == '4'" class="filter-item">
            <span class="span">规则类型
              <el-select v-model="listQuery.type" clearable placeholder="请输入规则类型" style="width: 200px">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option> </el-select></span>
          </div>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>
          <el-button v-if="listQuery.ruleType == '4'" plain v-waves v-permission="['rules:metadataRules:add']"
            class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
            {{ $t('table.add') }}
          </el-button>
          <el-button v-if="listQuery.ruleType == '4'" plain v-waves v-permission="['rules:metadataRules:delete']"
            class="filter-item" style="margin-left: 10px" type="danger" icon="el-icon-delete" @click="handleBathDelete">
            {{ $t('table.delete') }}
          </el-button>
          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest')
            }}</el-button>
        </div>

        <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list"
          border fit highlight-current-row style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" label="序号" align="center" width="50" />
          <el-table-column label="规则名称" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>

          <el-table-column label="规则级别" min-width="150px" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.level | dictLabel('level') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="规则状态" min-width="150px" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.status | dictLabel('is_use') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建日期" align="center" min-width="180px">
            <template slot-scope="{ row }">
              <span>{{ row.createDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="规则类型" align="center" min-width="150px" v-if="listQuery.ruleType == '4'">
            <template slot-scope="{ row }">
              <!-- <span>{{ row.type }}</span> -->
              <template v-for="(item, index) in dictList('quality_rule_type')">
                <span v-if="item.value == row.type">{{ item.label }}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="规则描述" align="center" min-width="150px" v-if="listQuery.ruleType != '4'">
            <template slot-scope="{ row }">
              <span>{{ row.descriptions }}</span>
            </template>
          </el-table-column>
          <el-table-column label="规则值" align="center" min-width="150px" v-if="listQuery.ruleType == '4'">
            <template slot-scope="{ row }">
              <span>{{ row.capitulation.value }}</span>
            </template>
          </el-table-column>
          <el-table-column label="规则条件" align="center" min-width="150px" v-if="listQuery.ruleType == '4'">
            <template slot-scope="{ row }">
              <span>{{ row.capitulation.whereType | dictLabel('sql_where_type') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" min-width="230"
            class-name="small-padding fixed-width" v-if="listQuery.ruleType == '4'">
            <template slot-scope="{ row }">
              <el-button v-permission="['rules:metadataRules:update']" type="text" size="small" icon="el-icon-edit"
                @click="handleUpdate(row)">
                {{ $t('table.edit') }}
              </el-button>
              <el-button v-permission="['rules:metadataRules:delete']" type="text" size="small" icon="el-icon-delete"
                class="delete-text-btn" @click="handleDelete(row, 'deleted')">
                {{ $t('table.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <import ref="import" template-url="/rules/metadataRules/template" import-url="/rules/metadataRules/import"
          @refreshList="getList" />
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
          :page-sizes="pageArray" @pagination="getList" />

        <metadata-rules-form ref="form" @refreshList="getList" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { deletemetadataRules, fetchmetadataRulesList, exportmetadataRules } from '@/api/rules/metadataRules'
import Import from '@/components/Import/import'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import metadataRulesForm from './metadataRulesForm'
import Pagination from '@/components/Pagination'

import { getDict, getDictOne } from '@/api/rules/metadataRules'
export default {
  name: 'metadataRulesList',
  components: { metadataRulesForm, Pagination, Import },
  directives: { waves, permission },
  data() {
    return {
      tableOneData: [],
      tableData: [],
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      downloadLoading: false,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        name: undefined,
        type: undefined,
        level: undefined,
        capitulation: undefined,
        status: undefined,
        page: 1,
        ruleType: '4', //默认展示自定义规则
        limit: this.$store.getters.defaultPageSize,
      },
      options: [
        { value: '1', label: '条件查询' },
        { value: '2', label: '正则表达式' },
      ],
      treeList: [],
      activeIndex: 4,
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },
  created() {
    this.treeList = this.dictList('rule_type')
    console.log(this.dictList('rule_type'))
    // getDict().then(res => {
    //   if (res.data.code === 200) {
    //     this.tableData = res.data.data
    //     console.log(this.tableData)
    //   }
    // })
    // getDictOne().then(res => {
    //   if (res.data.code === 200) {
    //     this.tableOneData = res.data.data
    //     console.log(this.tableData)
    //   }
    // })
    this.getList()
  },
  methods: {
    rowStyle({ row, rowIndex }) {
      if (this.listQuery.ruleType === row.value) {
        // 此处返回选中行的样式对象，按需设置
        return {
          'background-color': 'rgb(94, 180, 251)',
        }
      }
    },

    submitForm(data) {
      this.listQuery.ruleType = data.value
      this.handleFilter()
    },
    getChangeType(e) {
      for (var i = 0; i < this.List.length; i++) {
        if (this.List[i].value == e) {
          return this.List[i].label
        }
      }
    },
    getList() {
      this.listLoading = true
      fetchmetadataRulesList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletemetadataRules(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //重置
    handleRest() {
      this.listQuery.name = ""
      this.listQuery = {
        ruleType: this.listQuery.ruleType,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()

    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletemetadataRules(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleImport() {
      this.$refs.import.show()
    },
    handleExport() {
      this.downloadLoading = true
      exportmetadataRules(this.listQuery).then((response) => {
        this.downloadLoading = true
        if (response.data.code === 0) {
          import('@/vendor/Export2Excel').then((excel) => {
            excel.export_byte_to_excel(response.data.data.bytes, response.data.data.title)
            this.downloadLoading = false
          })
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 90px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}

.active {
  background-color: #f5f7fa;
}
</style>
